package com.future.lock.home.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.future.baselib.utils.DensityUtil;

/**
 * 带外圆环的imageView
 * Created by jniu on 2017/11/23.
 */

public class DrawImageView extends ImageView {

    private final int dp20;
    private Paint paint1;
    private Paint paint2;
    private Paint paint3;
    private Paint paint4;

    boolean isDown1 = false;
    boolean isDown2 = false;
    boolean isDown3 = false;
    int alpha1 = 0;
    int alpha2 = -32;
    int alpha3 = -32;
    private final int dp2;
    private final int dp16;
    private final int dp26;
    private final int dp32;


    public DrawImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        dp2 = DensityUtil.dip2px(context, 2);
        dp16 = DensityUtil.dip2px(context, 16);
        dp26 = DensityUtil.dip2px(context, 26);
        dp32 = DensityUtil.dip2px(context, 36);
        dp20 = DensityUtil.dip2px(context, 10);

        paint1 = new Paint();
        paint1.setAntiAlias(true);
        paint1.setStrokeWidth(dp2);
        paint1.setStyle(Paint.Style.STROKE);

        paint2 = new Paint();
        paint2.setAntiAlias(true);
        paint2.setStrokeWidth(dp2);
        paint2.setStyle(Paint.Style.STROKE);

        paint3 = new Paint();
        paint3.setAntiAlias(true);
        paint3.setStrokeWidth(dp2);
        paint3.setStyle(Paint.Style.STROKE);

        paint4 = new Paint();
        paint4.setAntiAlias(true);
        paint4.setStrokeWidth(dp20);
        paint4.setStyle(Paint.Style.STROKE);
        paint4.setColor(Color.parseColor("#F4F7FD"));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        paint1.setColor(Color.argb(alpha1<0?0:alpha1,0x64,0x8d,0xdd));
        int center = getWidth() / 2;
        canvas.drawCircle(center,center,center-dp26,paint1);

        paint2.setColor(Color.argb(alpha2<0?0:alpha2,0x64,0x8d,0xdd));
        canvas.drawCircle(center,center,center-dp16,paint2);

        paint3.setColor(Color.argb(alpha3<0?0:alpha3,0x64,0x8d,0xdd));
        canvas.drawCircle(center,center,center-dp2,paint3);

        canvas.drawCircle(center,center,center-dp32,paint4);

        postDelayed(runnable,10);
        super.onDraw(canvas);
    }

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (alpha1 >= 128) {
                isDown1 = true;
            }
            if (alpha1 <= 0){
                isDown1 = false;
            }
            if (isDown1){
                alpha1-=4;
            }else{
                alpha1+=4;
            }

            if (alpha2 >= 64) {
                isDown2 = true;
            }
            if (alpha2 <= 0){
                isDown2 = false;
            }
            if (isDown2){
                alpha2 -= 2;
            }else{
                alpha2 += 2;
            }

            if (alpha3 >= 32) {
                isDown3 = true;
            }
            if (alpha3 <= 0){
                isDown3 = false;
            }
            if (isDown3){
                alpha3--;
            }else{
                alpha3++;
            }
            postInvalidate();
        }
    };
}
